#
# "@(#) $Id: Player.py,v 1.1.1.1 2006-07-27 03:16:08 gioshe Exp $"
#
# This work is released under the GNU GPL, version 2 or later.
#
from Utils.Settings import *
import Engines.GSTPlayer
from Engines.GSTPlayer import GSTPlayer
import traceback

_canOverride = True
_currentPlayerTrack = None
_currentPlayerLibrary = None
_currentPlayerPlaylist = None
_playerStatus = None
_currentPlaylists = {}

class Player(QObject):
	def __init__(self):
		QObject.__init__(self)
		self.player=None
		self.loadFromPrefs()
			
	def loadFromPrefs(self):
		settings = Settings.settings()
		self.setShuffle(settings.get('Shuffle',False))
		self.setRepeat(settings.get('Repeat','off'))
		#playerEngine= settings.get('Engine','GST')
		self.player=GSTPlayer(self)
	
	def getShuffle(self):
		return self._shuffle
	def setShuffle(self,bool):
		self._shuffle = bool
		Settings.settings()['Shuffle'] = bool
	shuffle = property(getShuffle,setShuffle)
	
	def getRepeat(self):
		return self._repeat
	def setRepeat(self,repeat):
		self._repeat = repeat
		Settings.settings()['Repeat'] = repeat
	repeat = property(getRepeat,setRepeat)

	def volume(self):
		return self.player.volume()
	
	def setVolume(self,volume,fromObject = None):
		self.player.setVolume(volume,fromObject)
		
	def increaseVolume(self):
		self.setVolume(min(self.volume()+10,100))

	def decreaseVolume(self):
		self.setVolume(max(self.volume()-10,0))

	def setSpeed(self,speed,fromObject = None):
		self.player.setSpeed(speed,fromObject)

	def setEQ(self,eq):
		self.player.setEQ(eq)
		
	def customEvent(self,e):
		global _playerStatus
		global _currentPlayerTrack
		_playerStatus = e.status()
		#print "got event status",_playerStatus
		if _playerStatus and _currentPlayerTrack:
			status = _playerStatus["Status"]
			interrupted=_playerStatus['Interrupted']
			track = _currentPlayerTrack
			_playerStatus['Track']=track
			_playerStatus['Source']='Player'
			if status=="Playing":
				if _playerStatus['isPlaying']:
					track.playStatus = 'playing'
				else:
					track.playStatus = 'paused'
			elif status=='Next': 
				if _playerStatus["Interrupted"]==False:
					track.playCount = track.playCount+1
                                        track.playDate = time.gmtime(time.time())
				if not track.isStream: 
					if not self.nextTrack(): _playerStatus["Status"]='End' 
				else: _playerStatus["Status"]='End'
			
			if status=='End':
				print "Player: End",status
				track.playStatus = 'idle'
				if _playerStatus["Interrupted"]==False:
					track.playCount = track.playCount+1
					track.playDate = time.gmtime(time.time())
			#self.emit(PYSIGNAL('playback_status'),({"Status": _playerStatus["Status"],"isPlaying":_playerStatus['isPlaying']},None))
			self.emit(PYSIGNAL('playback_status'),(_playerStatus,None))

	def static_playTrack(track,library=None,playlist=None):
		global _currentPlayerTrack
		global _currentPlayerLibrary
		global _currentPlayerPlaylist
		global _canOverride
		_currentPlayerTrack=track
		_currentPlayerLibrary=library
		_currentPlayerPlaylist=playlist
		Player.singleton().player.playTrack(track,library,playlist)
		_canOverride = False
	playTrack = staticmethod(static_playTrack)
	
	def static_playerStatus():
		global _playerStatus
		return _playerStatus
		#return Player.singleton().player.status()
	status = staticmethod(static_playerStatus)
	
	def static_stop():
		pass
	stop = staticmethod(static_stop)

	def static_killCurrentPlayer():
		Player.singleton().player.killCurrentPlayer()
	killCurrentPlayer = staticmethod(static_killCurrentPlayer)

	def static_killCurrentPlayerIfCD():
		Player.singleton().player.killCurrentPlayerIfCD()
	killCurrentPlayerIfCD = staticmethod(static_killCurrentPlayerIfCD)

	def static_play():
		Player.singleton().player.play()
	play = staticmethod(static_play)
	
	def static_pause():
		Player.singleton().player.pause()
	pause = staticmethod(static_pause)

	def static_seek(time):
		Player.singleton().player.seek(time)
	seek = staticmethod(static_seek)

	def static_singleton():
		global _playerSingleton
		if _playerSingleton==None:
			_playerSingleton = Player()
		return _playerSingleton
	singleton = staticmethod(static_singleton)

	def static_killAll():
		Player.singleton().player.killAll()
	killAll = staticmethod(static_killAll)
	
	def static_setCurrentTrackList(library,playlist,tracks):
		global _currentPlaylists
		_currentPlaylists[playlist] = tracks
	setCurrentTrackList = staticmethod(static_setCurrentTrackList)

	def static_getCurrentTrackInfo():
		global _currentPlayerLibrary,_currentPlayerPlaylist,_currentPlayerTrack
		return (_currentPlayerTrack,_currentPlayerLibrary,_currentPlayerPlaylist)
	getCurrentTrackInfo = staticmethod(static_getCurrentTrackInfo)

	def static_playItemFrom(library,playlist):
		global _currentPlaylists
		tracks = _currentPlaylists.get(playlist,None)
		if tracks!=None:
			if len(tracks)>0:
				track = tracks[0]
				#print "playing track",track
				track.play(playlist)
		else:
			print "Tracks for",playlist.name,"not found"
		
	playItemFrom = staticmethod(static_playItemFrom)
	
	def static_nextTrack():
		global _currentPlayerLibrary,_currentPlayerPlaylist,_currentPlayerTrack
		tracks = _currentPlaylists.get(_currentPlayerPlaylist,None)
		trackTest = _currentPlayerPlaylist.getNextTrack(_currentPlayerTrack)
		if trackTest:
			trackTest.play(_currentPlayerPlaylist)
			return True
		else:
			return False
	nextTrack = staticmethod(static_nextTrack)

	def static_prevTrack():
		global _currentPlayerLibrary,_currentPlayerPlaylist,_currentPlayerTrack
		tracks = _currentPlaylists.get(_currentPlayerPlaylist,None)
		trackTest = _currentPlayerPlaylist.getPrevTrack(_currentPlayerTrack)
		if trackTest:
			trackTest.play(_currentPlayerPlaylist)
		else:
			pass #print "LGSTPlayer: can't find track in playlist"
	prevTrack = staticmethod(static_prevTrack)
	
	def static_shouldPlay():
		global _canOverride
		return _canOverride
	shouldPlay = staticmethod(static_shouldPlay)

	def static_selectedTrack():
		global _canOverride
		_canOverride = (_currentPlayerTrack==None or _currentPlayerTrack.playStatus=='paused')
	selectedTrack = staticmethod(static_selectedTrack)

	def static_isPlayingCDTrack():
		global _currentPlayerTrack
		return _currentPlayerTrack and _currentPlayerTrack.kind=='CD Track'
	isPlayingCDTrack = staticmethod(static_isPlayingCDTrack)
		
	def static_setCDPlayer(player):
		Player.singleton().player.setCDPlayer(player)
	setCDPlayer = staticmethod(static_setCDPlayer)

_playerSingleton =None
#"""
